package com.longdaji.decoration.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.Topic;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Kanade on 2018/3/7 0007.
 */

public class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.TopicHolder> {

    private List<Topic> mTopicList;
    private Context mContext;

    public TopicAdapter(List<Topic> topicList) {
        super();
        mTopicList = topicList;
    }

    class TopicHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_topic_picture)
        ImageView ivTopicPicture;
        @BindView(R.id.tv_topic)
        TextView tvTopic;
        @BindView(R.id.rl_topic_item)
        RelativeLayout rlTopicItem;

        public TopicHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.rl_topic_item)
        void onViewClicked(View view) {
            mOnItemClickListener.onItemClick(getAdapterPosition());
        }
    }

    @Override
    public TopicHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_topic, parent, false);
        TopicHolder holder = new TopicHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(TopicHolder holder, int position) {
        Topic topic = mTopicList.get(position);
        Glide.with(mContext).load(topic.getTopicUri()).into(holder.ivTopicPicture);
        holder.tvTopic.setText(topic.getTopic());
    }

    @Override
    public int getItemCount() {
        return mTopicList.size();
    }

    public void setData(List<Topic> list) {
        mTopicList.clear();
        mTopicList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData() {
        mTopicList.clear();
        notifyDataSetChanged();
    }

    private OnItemClickListener mOnItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
